package cn.whpu.service.impl;

import cn.whpu.dao.AccountDao;
import cn.whpu.domain.Account;
import cn.whpu.service.AccountService;
import cn.whpu.utils.TransactionManager;

import java.util.List;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl_LOD implements AccountService {

    private AccountDao accountDao;
    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public List<Account> findAllAccount() {
        try {
            //开启事务
            transactionManager.beginTransaction();
            //执行操作
            List<Account> accounts = accountDao.findAllAccount();
            //提交事务
            transactionManager.commit();
            //返回结果
            return accounts;
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放连接
            transactionManager.release();
        }
    }

    public Account findById(Integer accountId) {
        try {
            //开启事务
            transactionManager.beginTransaction();
            //执行操作
            Account account = accountDao.findById(accountId);
            //提交事务
            transactionManager.commit();
            //返回结果
            return account;
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
            throw new RuntimeException(e);
        }finally {
            //释放连接
            transactionManager.release();
        }
    }

    public void saveAccount(Account account) {
        try {
            //开启事务
            transactionManager.beginTransaction();
            //执行操作
            accountDao.saveAccount(account);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
        }finally {
            //释放连接
            transactionManager.release();
        }

    }

    public void updateAccount(Account account) {
        try {
            //开启事务
            transactionManager.beginTransaction();
            //执行操作
            accountDao.updateAccount(account);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
        }finally {
            //释放连接
            transactionManager.release();
        }

    }

    public void deleteAccount(Integer accountId) {
        try {
            //开启事务
            transactionManager.beginTransaction();
            //执行操作
            accountDao.deleteAccount(accountId);
            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
        }finally {
            //释放连接
            transactionManager.release();
        }

    }

    public void transfer(String sourceName, String targetName, Float money) {
        try {
            //开启事务
            transactionManager.beginTransaction();

            //执行操作
            //1.根据名称查询转出账户
            Account source = accountDao.findByName(sourceName);
            //2.根据名称查询转入账户
            Account target = accountDao.findByName(targetName);
            //3.转出账户扣钱
            source.setMoney(source.getMoney()-money);
            //4.转入账户加钱
            target.setMoney(target.getMoney()+money);
            //5.更新转出账户
            accountDao.updateAccount(source);

            int i =1/0;
            //6.更新转入账户
            accountDao.updateAccount(target);

            //提交事务
            transactionManager.commit();
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback();
            e.printStackTrace();
        }finally {
            //释放连接
            transactionManager.release();
        }


    }
}
